package db

import (
	"fmt"
	"github.com/elastic/go-elasticsearch/v8"
	"lib/config"
	"lib/log"
	"net/http"
)

var elasticsearchClient *elasticsearch.Client

// Redis 在中间件中初始化redis链接
func InitElasticsearch(es *config.Es) error {

	cfg := elasticsearch.Config{
		Addresses: []string{
			fmt.Sprintf("%s:%s", es.Address, es.Port), // 替换为你的Elasticsearch地址和端口
		},
		Username:               es.UserName,
		Password:               es.Password,
		CertificateFingerprint: es.CertificateFingerprint,
		Transport: &http.Transport{
			MaxConnsPerHost: 0,
		},
	}
	client, err := elasticsearch.NewClient(cfg)
	if err != nil {
		log.Fatalf("Error creating the client: %s", err)
	}

	elasticsearchClient = client
	return nil
}

func GetElasticsearchClient() *elasticsearch.Client {
	return elasticsearchClient
}
